package com.wbao.dianniu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.HeadData;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;


/**
 * 好友列表-
 *
 * @author yejianjun
 */
public class FriendListAdapter extends BaseAdapter implements View.OnClickListener {
    private List<HeadData> mList = new ArrayList<>();
    private Context mContext;
    LayoutInflater mInflater;
    /**
     * 事件回调监听
     */
    private OnItemClickListener mItemClickListener;


    public FriendListAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<HeadData> getList() {
        return mList;
    }

    public void clear() {
        if (null != mList) {
            mList.clear();
        }
    }

    public void setData(List<HeadData> list) {
        this.mList = list;
    }

    public void addData(List<HeadData> listData) {
        if (null == mList) {
            mList = new ArrayList<HeadData>();
        }
        for (int i = 0; i < listData.size(); i++) {
            mList.add(listData.get(i));
        }
    }

    public void deleteFriend(int friendId) {
        for (int i = 0; i < mList.size(); i++) {
            if (friendId == mList.get(i).friendId) {
                mList.remove(i);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void cancelFollow(int followId) {
        for (int i = 0; i < mList.size(); i++) {
            if (followId == mList.get(i).followId) {
                mList.remove(i);
                notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.friend_list_item, null);
            holder = new Holder();
            holder.allLayout = (LinearLayout) convertView.findViewById(R.id.friend_list_layout);
            holder.ivHead = (ImageView) convertView.findViewById(R.id.friend_list_iv);
            holder.tvName = (TextView) convertView.findViewById(R.id.friend_list_name);
            holder.tvRemark = (TextView) convertView.findViewById(R.id.friend_list_remark);
            holder.tvLabel = (TextView) convertView.findViewById(R.id.friend_list_label);
            holder.sexIV = (ImageView) convertView.findViewById(R.id.friend_list_sex);
            holder.addV = (ImageView) convertView.findViewById(R.id.friend_list_isIndustry);
            holder.partnerIV = (ImageView) convertView.findViewById(R.id.friend_list_partner);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        HeadData data = mList.get(position);
        if (TextUtils.isEmpty(data.headPic)) {
            holder.ivHead.setImageResource(R.drawable.default_head_icon);
        } else {
            PictureDownloadUtils.displayHeadImage(mContext, holder.ivHead, data.headPic);
        }
        holder.tvName.setText(data.realName == null ? "" : data.realName);
        if (TextUtils.isEmpty(data.remark)) {
            holder.tvRemark.setText("");
        } else {
            String title = String.format(mContext.getResources().getString(R.string.remark_name), data.remark);
            holder.tvRemark.setText(title);
        }
        holder.tvLabel.setText(data.label == null ? "" : data.label);
        Utils.showSex(mContext, data.sex, holder.sexIV);
        Utils.showPartner(mContext, data.partner, holder.partnerIV);
        if (data.isIndustryAuthority) {
            holder.addV.setVisibility(View.VISIBLE);
        } else {
            holder.addV.setVisibility(View.GONE);
        }
        holder.allLayout.setOnClickListener(this);
        holder.allLayout.setTag(R.id.friend_list_layout, position);
        return convertView;
    }

    class Holder {
        LinearLayout allLayout;
        ImageView ivHead;
        TextView tvName;
        TextView tvRemark;
        ImageView sexIV;
        ImageView partnerIV;
        TextView tvLabel;
        ImageView addV;
    }


    @Override
    public void onClick(View v) {
        if (null != mItemClickListener)
            mItemClickListener.itemClick(v);
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void itemClick(View view);
    }

}